#include"stu.h"
#include<stddef.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int insert_student(struct student **p, int *nmemb, struct student data){
	*p = realloc(*p, (*nmemb + 1) * sizeof(struct student));
	if(NULL == p){
		return -1;
	}
	(*p)[*nmemb] = data;
	*nmemb = *nmemb + 1;
	return 0;	
}

int delete_student(struct student **p, int *nmemb, const void *key, int (*cmp)(const void *data, const void *key))
{
	  int i;
	  for(i = 0; i < *nmemb; i++){
	  	if(cmp((char *)*p + i * sizeof(struct student), key) == 0){
			break;
		}
	  }
	  if(i != *nmemb){
	  	memmove((char *)*p + i * sizeof(struct student), (char *)*p + (i + 1) * sizeof(struct student), (*nmemb - (i + 1)) * sizeof(struct student));
		*nmemb = *nmemb - 1;
		return 0;
	  }
	  return -1;
}


void show_students(const struct student *p, int nmemb){
	for(int i = 0; i < nmemb; i++){
		printf("%d %s %d\n", (p+i)->id, (p+i)->name, (p+i)->score);
	}
	printf("\n");
}

